package com.tianhua.javs.core.engine.pipeline;

import com.tianhua.javs.client.bean.*;
import com.tianhua.javs.client.enums.BusinessKeyWardEnum;
import com.tianhua.javs.client.enums.SegmentSceneEnum;
import com.tianhua.javs.client.enums.VariablePoolTypeEnum;
import com.tianhua.javs.client.model.AbstractClassModel;
import com.tianhua.javs.client.model.MethodModel;
import com.tianhua.javs.core.constant.TagConstants;
import com.tianhua.javs.core.engine.PipelineTranslator;
import com.tianhua.javs.core.engine.handler.BOHandler;
import com.tianhua.javs.core.engine.handler.CMDHandler;
import com.tianhua.javs.core.engine.handler.DOHandler;
import com.tianhua.javs.core.factory.ParamReferFactory;
import com.tianhua.javs.core.func.script.IBusinessFunc;
import com.tianhua.javs.core.parse.JavsContentParseService;
import com.tianhua.javs.core.parse.SegmentSpreadParseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Description:javs 业务关键字或者关键字函数处理管道
 * date: 2022/2/11
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Component(value = "businessPipelineTranslator")
public class BusinessPipelineTranslator implements PipelineTranslator {

    @Resource(name = "boFunc")
    private IBusinessFunc boFunc;

    @Resource(name = "doFunc")
    private IBusinessFunc doFunc;

    @Resource(name = "cmdFunc")
    private IBusinessFunc cmdFunc;

    @Autowired
    private ParamReferFactory paramReferFactory;

    @Autowired
    private SegmentSpreadParseService segmentSpreadParseService;


    @Autowired
    private BOHandler boHandler;

    @Autowired
    private DOHandler doHandler;

    @Autowired
    private CMDHandler cmdHandler;

    @Override
    public String translateKeyWard(JavsTranslateContextBean translateContextBean, String javsShell, String keyWard) {
        List<ScriptVarMappingBean> businessVarPool = translateContextBean.getCurrentScriptMethodBean().getScriptVarMap().get(VariablePoolTypeEnum.BUSINESS_VAR.getCode());
        if (businessVarPool == null) {
            return keyWard;
        }
        String replaceSegment = replace(businessVarPool, keyWard);
        //对return语句进行再解析
        if (replaceSegment.trim().startsWith(TagConstants.RETURN)) {
            String newKeyWard = keyWard.replaceFirst(TagConstants.RETURN, "").trim();

            String[] referArr = newKeyWard.split("\\.");
            String varCode = referArr[0];
            if (varCode.toLowerCase().endsWith(BusinessKeyWardEnum.BO.getKeyWardCode())) {
                TranslateResultBean translateResultBean = boHandler.handleTranslate(javsShell, keyWard, translateContextBean, SegmentSceneEnum.RETURN);
                if(translateResultBean.isMatch()){
                    return translateResultBean.getSegment();
                }
            }

            if (varCode.toLowerCase().endsWith(BusinessKeyWardEnum.DO.getKeyWardCode())) {
                TranslateResultBean translateResultBean = doHandler.handleTranslate(javsShell, keyWard, translateContextBean, SegmentSceneEnum.RETURN);
                if(translateResultBean.isMatch()){
                    return translateResultBean.getSegment();
                }
            }

            if (varCode.toLowerCase().endsWith(BusinessKeyWardEnum.CMD.getKeyWardCode())) {
                TranslateResultBean translateResultBean = cmdHandler.handleTranslate(javsShell, keyWard, translateContextBean, SegmentSceneEnum.RETURN);
                if(translateResultBean.isMatch()){
                    return translateResultBean.getSegment();
                }
            }
        }

        return replaceSegment;
    }

    private String replace(List<ScriptVarMappingBean> businessVarPool, String segment) {
        String tempSegment = segment;
        for (ScriptVarMappingBean scriptVarMappingBean : businessVarPool) {
            tempSegment = tempSegment.replace(scriptVarMappingBean.getJavsVarCode(), scriptVarMappingBean.getJavaCode());
        }

        return tempSegment;
    }
}
